﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Message.SMS.SGIP.Commands
{
    public class ReportCommand : Command
    {
        #region 变量与属性
        private CommandSequence _SubmitSequenceNumber;
        /// <summary>
        /// 该命令所涉及的Submit或deliver命令的序列号
        /// </summary>
        public CommandSequence SubmitSequenceNumber
        {
            get { return _SubmitSequenceNumber; }
            set { _SubmitSequenceNumber = value; }
        }

        private ReportType _ReportType = ReportType.Submit;
        /// <summary>
        /// Report命令类型
        /// </summary>
        public ReportType ReportType
        {
            get { return _ReportType; }
            set { _ReportType = value; }
        }

        private string _UserNumber = "";
        /// <summary>
        /// 接收短消息的手机号，手机号码前加“86”国别标志
        /// </summary>
        public string UserNumber
        {
            get { return _UserNumber; }
            set { _UserNumber = value; }
        }

        private ReportState _State = ReportState.Success;
        /// <summary>
        /// 该命令所涉及的短消息的当前执行状态
        /// </summary>
        public ReportState State
        {
            get { return _State; }
            set { _State = value; }
        }

        private ErrorCode _ErrorCode = ErrorCode.Success;
        /// <summary>
        /// 当State=2时为错误码值，否则为0
        /// </summary>
        public ErrorCode ErrorCode
        {
            get { return _ErrorCode; }
            set { _ErrorCode = value; }
        }

        private string _Reserve = "";
        /// <summary>
        /// 保留，扩展用
        /// </summary>
        public string Reserve
        {
            get { return _Reserve; }
            set { _Reserve = value; }
        }
        #endregion

        public override uint BodyLength
        {
            get { return 12 + 1 + 21 + 1 + 1 + 8; }
        }

        public override SGIPCommandType CommandType
        {
            get { return SGIPCommandType.Report; }
        }

        #region 构造函数
        public ReportCommand()
        {
        }

        public ReportCommand(byte[] bytes)
            : base(bytes)
        {
        }

        public ReportCommand(byte[] bytes, int startIndex)
            : base(bytes, startIndex)
        {
        }

        public ReportCommand(CommandSequence sequence)
            : base(sequence)
        {
        } 
        #endregion

        protected override void BytesTo(byte[] bytes, int startIndex)
        {
            int i = (int)CommandHeader.HeaderLength;
            base.BytesTo(bytes, startIndex);
            _SubmitSequenceNumber = new CommandSequence(bytes, startIndex);
            i += 12;
            _ReportType = (SGIP.ReportType)bytes[i];
            i++;
            _UserNumber = ByteStringConvert.Bytes2String(bytes, i, 21, MessageCoding.ASCII).Trim('\0');
            i += 21;
            _State = (ReportState)bytes[i];
            i++;
            _ErrorCode = (SGIP.ErrorCode)bytes[i];
            i++;
            Reserve = ByteStringConvert.Bytes2String(bytes, i, 8, MessageCoding.ASCII).TrimEnd('\0');
        }

        public override byte[] ToBytes()
        {
            int i = (int)CommandHeader.HeaderLength;
            byte[] bytes = base.ToBytes();
            this._SubmitSequenceNumber.ToByte().CopyTo(bytes, i);
            i += 12;
            bytes[i] = (byte)this._ReportType;
            i++;
            ByteStringConvert.String2Bytes(_UserNumber.PadRight(21, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            i += 21;
            bytes[i] = (byte)this._State;
            i++;
            bytes[i] = (byte)this._ErrorCode;
            i++;
            ByteStringConvert.String2Bytes(_Reserve.PadRight(8, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            return bytes;
        }
    }
}
